﻿using System;
using System.Text;

using Microsoft.VisualStudio.Shell.Interop;

namespace Microsoft.Samples.DslTools.ConfigurationSectionDesigner
{
    // This custom partial class handles automatically transforming all templates when the diagram is saved.

	internal partial class ConfigurationSectionDesignerDocData
    {
        /// <summary>
        /// Handle when document has been saved
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDocumentSaved(EventArgs e)
        {
            bool shouldTransformTemplates = true;

            // Check with the root model (the diagram) if the user allows the auto-transform.
            ConfigurationSectionModel model = this.RootElement as ConfigurationSectionModel;
            if (model != null)
            {
                shouldTransformTemplates = model.AutoTransformTemplates;
            }

            // If allowed, execute the text transformation command through Visual Studio's command window.
            if (shouldTransformTemplates)
            {
                IVsCommandWindow commandWindow = (IVsCommandWindow)this.ServiceProvider.GetService(typeof(IVsCommandWindow));
                if (commandWindow != null)
                {
                    commandWindow.ExecuteCommand("TextTransformation.TransformAllTemplates");
                }
            }

            // Always call the base class method.
            base.OnDocumentSaved(e);
        }
    }
}
